package com.architecture.core.utils;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-5-16
 * Time: 上午10:34
 * To change this template use File | Settings | File Templates.
 */
public enum ConfigUtil {
    //获取默认系统时间格式
    W_DEFAULT_DATE_FORMAT("config.web.date_formats.default_date_format"),

    //默认每页取得多少记录
    W_DEFAULT_PAGE_SIZE("config.web.page_size"),

    //下标显示长度
    W_DEFAULT_PAGE_INDEX_SIZE("config.web.page_index_size"),

    WRONG_NUM("config.sys.wrong_num"),

    SUPER_ADMIN_NAME("config.sys.super_admin"),

    D_S_I_QUERY_GROUP_NUM("config.db.sql.initData.query.group"),
    D_S_I_QUERY_USER_NUM("config.db.sql.initData.query.user"),
    D_S_I_QUERY_CATEGORY_NUM("config.db.sql.initData.query.category"),
    D_S_I_QUERY_ROLE_NUM("config.db.sql.initData.query.role"),
    D_S_I_QUERY_PERMISSION_NUM("config.db.sql.initData.query.permission"),
    D_S_I_QUERY_RES_NUM("config.db.sql.initData.query.permission"),
    D_S_I_QUERY_U_C_NUM("config.db.sql.initData.query.user_category"),
    D_S_I_QUERY_P_R_NUM("config.db.sql.initData.query.permission_role"),
    D_S_I_QUERY_R_R_NUM("config.db.sql.initData.query.role_role"),
    D_S_I_QUERY_P_RES_NUM("config.db.sql.initData.query.permission_res"),

    D_S_I_INSERT_GROUP("config.db.sql.initData.insert.group.e"),
    D_S_I_INSERT_USER("config.db.sql.initData.insert.user.e"),
    D_S_I_INSERT_CATEGORY("config.db.sql.initData.insert.category.e"),
    D_S_I_INSERT_ROLE("config.db.sql.initData.insert.role.e"),
    D_S_I_INSERT_PERMISSION("config.db.sql.initData.insert.permission.e"),
    D_S_I_INSERT_RES("config.db.sql.initData.insert.res.e"),
    D_S_I_INSERT_U_C("config.db.sql.initData.insert.user_category.e"),
    D_S_I_INSERT_P_R("config.db.sql.initData.insert.permission_role.e"),
    D_S_I_INSERT_R_R("config.db.sql.initData.insert.role_role.e"),
    D_S_I_INSERT_P_RES("config.db.sql.initData.insert.permission_res.e"),


    //hql

    D_QUERY_PERMISSION_ROLE("config.db.hql.query_permission_role"),

    D_DEFAULT_CONDITION("config.db.hql.default_hql_condition"),

    D_DEL_ENTITY_BY_ID("config.db.hql.del_entity_by_id"),

//    D_QUERY_RES_BY_G("config.db.hql.query.res.queryResByG"),
//    D_QUERY_RES_BY_G_U("config.db.hql.query.res.queryResByGU"),
//    D_QUERY_RES_BY_G_C("config.db.hql.query.res.queryResByGC"),
//    D_QUERY_RES_BY_G_P("config.db.hql.query.res.queryResByGP"),
//    D_QUERY_RES_BY_G_U_C("config.db.hql.query.res.queryResByGUC"),
//    D_QUERY_RES_BY_G_U_C_P("config.db.hql.query.res.queryResByGUC"),
//    D_QUERY_RES_BY_U("config.db.hql.query.res.queryResByU"),
//    D_QUERY_RES_BY_U_C("config.db.hql.query.res.queryResByUC"),
//    D_QUERY_RES_BY_U_P("config.db.hql.query.res.queryResByUP"),
//    D_QUERY_RES_BY_U_C_P("config.db.hql.query.res.queryResByUCP"),
//    D_QUERY_RES_BY_C("config.db.hql.query.res.queryResByC"),
//    D_QUERY_RES_BY_C_P("config.db.hql.query.res.queryResByCP"),
    D_QUERY_RES_BY_P("config.db.hql.query.res.queryResByP"),
//    D_QUERY_PERMIT_BY_R("config.db.hql.query.permit.queryPermitByR"),
//    D_QUERY_PERMIT_BY_U("config.db.hql.query.permit.queryPermitByU"),
//    D_QUERY_PERMIT_BY_U_C("config.db.hql.query.permit.queryPermitByUC"),
//    D_QUERY_PERMIT_BY_C("config.db.hql.query.permit.queryPermitByC"),
//    D_QUERY_PERMIT_BY_G("config.db.hql.query.permit.queryPermitByG"),
//    D_QUERY_PERMIT_BY_G_U("config.db.hql.query.permit.queryPermitByGU"),
//    D_QUERY_PERMIT_BY_G_C("config.db.hql.query.permit.queryPermitByGC"),
//    D_QUERY_PERMIT_BY_G_U_C("config.db.hql.query.permit.queryPermitByGUC"),
    D_QUERY_CATEGORY_BY_G("config.db.hql.query.category.queryCategoryByG"),
    D_QUERY_CATEGORY_BY_U("config.db.hql.query.category.queryCategoryByU"),
    D_QUERY_CATEGORY_BY_G_U("config.db.hql.query.category.queryCategoryByGU"),
    D_QUERY_ROLE_BY_G("config.db.hql.query.role.queryRoleByG"),
    D_QUERY_ROLE_BY_G_U("config.db.hql.query.role.queryRoleByGU"),
    D_QUERY_ROLE_BY_G_C("config.db.hql.query.role.queryRoleByGC"),
    D_QUERY_ROLE_BY_G_U_C("config.db.hql.query.role.queryRoleByGUC"),
    D_QUERY_ROLE_BY_U("config.db.hql.query.role.queryRoleByU"),
    D_QUERY_ROLE_BY_U_C("config.db.hql.query.role.queryRoleByUC"),
    D_QUERY_ROLE_BY_C("config.db.hql.query.role.queryRoleByC"),
//    D_QUERY_ROLE_BY_P("config.db.hql.query.role.queryRoleByP")

    D_QUERY_ALL_CONFIG("config.db.hql.query_all_config"),

    C_TABLE_CODE("config.code.table.Group"),
    C_TABLE_USER("config.code.table.User"),
    C_TABLE_CATEGORY("config.code.table.Category"),
    C_TABLE_PERMISSION_CODE("config.code.table.PermissionRole"),
    C_TABLE_PERMISSION("config.code.table.Permission"),
    C_TABLE_RESOURCE("config.code.table.Resource")

    //hql
    ;

    private static final String SYS_CONFIG_PATH = "sys-config.xml";

    private static final String SYS_INIT_EXCEPT = "系统配置环境初始化异常。";

    private final String key;
    private static XMLConfiguration configuration;
    private final static Logger logger = Logger.getLogger(ConfigUtil.class);

    static {
        try {
            configuration = new XMLConfiguration();
            configuration.setDelimiterParsingDisabled(true);
            configuration.setAttributeSplittingDisabled(true);
            configuration.load(SYS_CONFIG_PATH);
        } catch (ConfigurationException e) {
            logger.warn(SYS_INIT_EXCEPT);
        }
    }

    private ConfigUtil(String key) {
        this.key = key;
    }

    public static boolean getBoolean(ConfigUtil config) {
        return configuration.getBoolean(config.toString());
    }

    public static short getShort(ConfigUtil config) {
        return configuration.getShort(config.toString());
    }

    public static int getInt(ConfigUtil config) {
        return configuration.getInt(config.toString());
    }

    public static long getLong(ConfigUtil config) {
        return configuration.getLong(config.toString());
    }

    public static float getFloat(ConfigUtil config) {
        return configuration.getFloat(config.toString());
    }

    public static Double getDouble(ConfigUtil config) {
        return configuration.getDouble(config.toString());
    }

    public static String getString(ConfigUtil config) {
        return configuration.getString(config.toString());
    }

    public static List<String> getStringList(ConfigUtil config) {
        List<Object> objList = configuration.getList(config.toString());

        List<String> resultStr = new ArrayList<String>();
        for (Object obj : objList) {
            if (obj instanceof String) {
                resultStr.add((String) obj);
            }
        }
        return resultStr;
    }

    public static Map<String, String> getMap(ConfigUtil config) {
        List<HierarchicalConfiguration> subConfig = configuration.configurationsAt(config.toString());
        Map<String, String> map = new HashMap<String, String>();
        for (HierarchicalConfiguration sub : subConfig) {
            map.put(sub.getString("key"), sub.getString("value"));           //在配置文件中，element必须以KEY和VALUE命名
        }
        return map;
    }

    @Override
    public String toString() {
        return key;
    }
}
